package com.yzh.blog.service.impl;

import com.yzh.blog.constant.MessageConstant;
import com.yzh.blog.dao.ArticleDao;
import com.yzh.blog.entity.Result;
import com.yzh.blog.pojo.Article;
import com.yzh.blog.service.inter.ArticleService;
import com.yzh.blog.utils.QiNiuTools;
import com.yzh.blog.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author yzh
 * @version 1.0
 * @description: TODO
 * @date 2022/4/15 11:19
 */
@Service
// @Transactional
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleDao articleDao;

    /**
     * 获取文章列表，后期可修改为分页查询
     *
     * @return
     */
    @Override
    public Result getArticleList() {
        try {
            List<Article> articleList = articleDao.selectArticleList();
            if (articleList.size() > 0) {
                for (Article article : articleList) {
                    List<String> tags = articleDao.selectTags(article.getId());
                    article.setTags(tags);
                }
                return new Result(true, MessageConstant.GET_ARTICLE_LIST_SUCCESS, articleList);
            }
            return new Result(false, MessageConstant.GET_ARTICLE_LIST_EMPTY);
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_ARTICLE_LIST_FAIL);
        }
    }

    /**
     * 增加一篇文章
     *
     * @param map 前端传递的文章信息
     * @return
     */
    @Override
    public Result addArticle(Map<String, Object> map) {
        try {
            // 获取文章信息
            Article article = new Article();
            article.setTitle((String) map.get("title"));
            article.setSummary((String) map.get("summary"));
            article.setCategory((String) map.get("type"));
            article.setBody((String) map.get("content"));
            article.setTags((List<String>) map.get("tagsChecked"));
            article.setHeadImg((String) map.get("imageUrl"));
            article.setBody((String) map.get("content"));
            article.setCreateDate(Tools.getCurrentDate());
            article.setId(UUID.randomUUID().toString());

            // 插入文章信息
            articleDao.insertArticle(article);
            // 插入类别
            int category_id = articleDao.selectCategoryIdByArticleCatgoryName(article.getCategory());
            articleDao.updateArticleType(article.getId(), category_id);
            // 插入标签
            for (String tag : article.getTags()) {
                articleDao.InsertArticleTags(article.getId(), tag);
            }
            return new Result(true, MessageConstant.ADD_ARTICLE_SUCCESS);
        } catch (Exception e) {

            return new Result(false, MessageConstant.ADD_ARTICLE_FAIL);
        }
    }

    /**
     * 获取所有文章类别
     *
     * @return
     */
    @Override
    public Result getArticleTypes() {
        List<String> types = articleDao.selectAllCategory();
        Result result;
        try {
            if (types.size() > 0)
                result = new Result(true, MessageConstant.GET_ARTICLE_CATEGORY_SUCCESS, types);
            else
                result = new Result(false, MessageConstant.GET_ARTICLE_CATEGORY_EMPTY);
        } catch (Exception e) {
            result = new Result(false, MessageConstant.GET_ARTICLE_CATEGORY_FAIL);
        }
        return result;
    }
}
